package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.dingreading.cloud.soms.dto.MemberFollowDto;
import com.dingreading.cloud.soms.entity.MemberFollow;
import com.dingreading.cloud.soms.entity.table.MemberFollowTableDef;
import com.dingreading.cloud.soms.mapper.MemberFollowMapper;
import com.dingreading.cloud.soms.service.MemberFollowService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.soms.entity.table.MembersTableDef.members;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class MemberFollowServiceImpl extends BaseServiceImpl<MemberFollowMapper, MemberFollow, MemberFollowTableDef> implements MemberFollowService {

    @Override
    protected MemberFollowTableDef getTable() {
        return MemberFollowTableDef.memberFollow;
    }

    @Override
    public Page<MemberFollowDto> pageList(
            PageUtil pageUtil, String storeUid, String memberUid, String followUid,
            String beginDate, String endDate, String keyword, Integer source, String staffUid
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(memberUid))
            condition.and(table.memberUid.eq(memberUid));
        if (StringUtils.isNotBlank(followUid))
            condition.and(table.followTypeUid.eq(followUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.memberName.like(keyword));
        if (null != source)
            condition.and(table.source.eq(source));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.followTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));
        if (StringUtils.isNotBlank(staffUid))
            condition.and(table.followStaffUid.eq(staffUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns, members.memberName.as("newMemberName"))
                .from(table)
                .leftJoin(members).on(table.memberUid.eq(members.uid))
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, MemberFollowDto.class);
    }

    @Override
    public List<MemberFollow> getList(String storeUid, String memberUid, String followUid, String beginDate, String endDate,
                                      String keyword, Integer source, String staffUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(memberUid))
            condition.and(table.memberUid.eq(memberUid));
        if (StringUtils.isNotBlank(followUid))
            condition.and(table.followTypeUid.eq(followUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.memberName.like(keyword));
        if (null != source)
            condition.and(table.source.eq(source));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.followTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));
        if (StringUtils.isNotBlank(staffUid))
            condition.and(table.followStaffUid.eq(staffUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());
        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public boolean haveFollowTypeUid(String storeUid, String followTypeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.followTypeUid.eq(followTypeUid));
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        return exists(condition);
    }

    @Override
    public MemberFollow getBean(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public MemberFollowDto getDto(String uid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.uid.eq(uid));
        return getMapper().selectOneByQueryAs(wrapper, MemberFollowDto.class);
    }

    @Override
    public int dropBean(String storeUid, String memberUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.memberUid.eq(memberUid))
                .and(table.uid.eq(uid))
                .and(table.storeUid.eq(storeUid));
        return getMapper().deleteByCondition(condition);
    }

}
